
const express = require("express");
const router = express.Router();
const diarybookModel = require("../model/diarybook.js");
const userModel = require("../model/user.js");
const { send , sendErr} = require("./send.js");

router.get('/', async function (req, res) { //获取用户的所有日记本及其内数据
    try {
        let { authorId , userId } = req.query;
        let diarybooks = await diarybookModel.find({ authorId }, { __v: 0 });
        if(authorId !== userId){
            for(let i = 0 ; i < diarybooks ; i++){
                if (diarybooks[i].private){
                    diarybooks.splice(i-- , 1);
                }
            }
        }
        send(res, {
            data: {
                diarybooks
            }
        });
    } catch (error) {
        console.log(error);
        sendErr(res, { msg: "传入的数据可能有问题" });
    }
})

router.get("/content", async function (req, res) { //获取单个日记本内数据
    try {
        let { diarybookId } = req.query;
        let diarybook = await diarybookModel.findById(diarybookId, { __v: 0 });
        if (diarybook) send(res, { data: { diarybook } });
        else sendErr(res, { msg: "没有该日记本" });
    } catch (error) {
        console.log(error);
        sendErr(res, { msg: "传入的数据可能有问题" });
    }
})

router.post("/create", async function (req, res) { //创建日记本
    try {
        let { authorId, title, private,
            description, createAt, overduetime } = req.body;
        let obj = {};
        if (await userModel.findById(authorId)) {
            obj.authorId = authorId;
            obj.overduetime = overduetime;
            if (title) obj.title = title;
            if (private) obj.private = private;
            if (description) obj.description = description;
            if (createAt) obj.createAt = createAt;

            let diarybook = await diarybookModel.create({ ...obj });

            if (diarybook) {
                await userModel.findByIdAndUpdate(authorId, { $push: { diarybookId: diarybook._id } });
                send(res, { data: { diarybook } });
            }
            else send(res, { msg: "创建失败" });
        } else {
            sendErr(res, { msg: "用户不存在" });
        }
    } catch (error) {
        console.log(error);
        sendErr(res, { msg: "传入的数据可能有问题" });
    }
})

router.post("/revise", async function (req, res) { //修改日记本
    try {
        let { diarybookId, title, description, private } = req.body;
        let obj = {};
        if (title) obj.title = title;
        if (description) obj.description = description;
        if (private !== undefined) obj.private = private;
        let diarybook = await diarybookModel.findByIdAndUpdate(diarybookId, { ...obj });

        if (diarybook) send(res);
        else sendErr(res, { msg: "修改失败" });
    } catch (error) {
        console.log(error);
        sendErr(res, { msg: "传入的数据可能有问题" });
    }
})

router.delete("/:id", async function (req, res) { //删除日记本
    try {
        let { id } = req.params;
        let diarybook = await diarybookModel.findById(id);
        if (diarybook) {
            if (diarybook.diaryItems.length) sendErr(res, { msg: "只能删除空日记本" });
            else {
                await diarybookModel.findByIdAndDelete(id);
                send(res);
            }
        } else {
            sendErr(res, { msg: "日记本不存在" });
        }
    } catch (error) {
        console.log(error);
        sendErr(res, { msg: "传入的数据可能有问题" });
    }
})


module.exports = router;